package com.thinkin.jdbc.driver;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

/**
 * 
 * @author cyt
 * 
 */
public final class JdbcDriverUtils {
	private static JdbcDriverUtils jdbcInstance;
	private static String driver;
	private static String url;
	private static String user;
	private static String password;
	private static BasicDataSource dbs;

	private JdbcDriverUtils() {
		java.util.Properties pes = new java.util.Properties();
		try {
			pes.load(JdbcDriverUtils.class.getClassLoader()
					.getResourceAsStream("jdbc.properties"));
			driver = pes.getProperty("jdbc.driverClassName");
			url = pes.getProperty("jdbc.url");
			user = pes.getProperty("jdbc.username");
			password = pes.getProperty("jdbc.password");

			dbs = new BasicDataSource();
			dbs.setDriverClassName(driver);
			dbs.setUrl(url);
			dbs.setUsername(user);
			dbs.setPassword(password);
			dbs.setInitialSize(3);
			dbs.setMinIdle(3);
			dbs.setMaxActive(1000);
			dbs.setMaxWait(6000);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}

	}

	/*
	 *
	 */
	public static JdbcDriverUtils getJdbcInstance() {
		/**
		 * 
		 */
		if (jdbcInstance == null) {
			synchronized (JdbcDriverUtils.class) {
				if (jdbcInstance == null) {
					jdbcInstance = new JdbcDriverUtils();
				}

			}
		}
		return jdbcInstance;
	}

	/**
	 * 
	 * 
	 * @return
	 * @throws Exception
	 * @throws Exception
	 */
	public Connection getConn() {
		Connection conn = null;
		try {
			conn = (Connection) dbs.getConnection();
			System.out.println("连接成功");
		
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("连接失败");
		}

		return conn;
	}
		public static void main(String[] args)
		{
			JdbcDriverUtils link=JdbcDriverUtils.getJdbcInstance();
			link.getConn();
			
					
		}
}
